addH1PathElement = function(txt) {
  var div = document.createElement('div');
    div.appendChild(document.createTextNode('›'));
  $("#path").append(div);
  var div = document.createElement('div');
      div.appendChild(document.createTextNode(txt));
  $("#path").append(div);
}

setArboPath = function(elt) {
	if (elt == null) {
		elt = $("#tree li.current > div:first");
		$("#path").empty();
		addH1PathElement($("#menu a.current").text());
		setArboPath(elt);
	} else if ($(elt).attr('id') != 'tree-0') {
    var parentDiv = $(elt).parent('li').parent('ul').prev();
    var parentPath = setArboPath(parentDiv);
    
    var txt = $(elt).children('span').text();
    addH1PathElement(txt);
  }
}

treeSetup = function(elt) {
  var parentId = $(elt).attr('id').substring(5);
	$("#tree .current").removeClass('current');
	$(elt).parent('li').addClass('current');
  $("#path").empty();
  addH1PathElement($("#menu a.current").text());
  setArboPath(elt);
  
  $("input[name=parentId]").val(parentId);
	changePanes();
}

treeCreateElement = function(id, name, children) {
  var img = document.createElement('img');
  if (children != 0)
  {
      img.src = domainImg + "puce-plus.gif";
      img.alt = "+";
  }
  else
  {
      img.src = domainImg + "puce-point.gif";
      img.alt = ".";
  }
  
  var span = document.createElement('span');
      span.appendChild(document.createTextNode(name));
  
  var div = document.createElement('div');
      div.setAttribute('id', 'tree-' + id);
      div.appendChild(img);
      div.appendChild(span);
      
  $(img).click(function() { treeExpand(div); });
  $(span).click(function() { treeSetup(div); });
  
  var li = document.createElement('li');
      li.appendChild(div);
  return li;
}

treeExpand = function(elt) {
  treeSetup(elt);
  var sub = $(elt).next('ul');
  if ($(sub).length != 0) {
    if ($(sub).is(':hidden')) {
      $(elt).children('img').attr('src', domainImg + 'puce-moins.gif');
      $(elt).children('img').attr('alt', '-');
      $(sub).slideDown();
    } else {
      $(sub).slideUp();
      $(elt).children('img').attr('src', domainImg + 'puce-plus.gif');
      $(elt).children('img').attr('alt', '+');
    }
  } else {
    var parentId = $(elt).attr('id').substring(5);
    $.post("getfolders.htm", { parentId:parentId }, function(xml) {
      var ul = document.createElement('ul');
      $(xml).find('item').each(function() {
        var id = $(this).find('id').text();
        var name = $(this).find('name').text();
        var children = $(this).find('children').text();
        var li = treeCreateElement(id, name, children);
        ul.appendChild(li);
      });
      
      if (ul.children.length > 0) {
        $(ul).hide();
        $(elt).after(ul);
        $(elt).children('img').attr('src', domainImg + 'puce-moins.gif');
        $(elt).children('img').attr('alt', '-');
        $(ul).slideDown();
      }
    });
  }
}

submitNewFolder = function(fieldset) {
  var vParentId = $(fieldset).children('input[name=parentId]').val();
  var vFolder   = $(fieldset).children('input[name=folder]').val();
  $.post("addfolder.htm", { parentId:vParentId, name:vFolder }, function(xml) {
    var result = $(xml).find('result').text() == '1' ? true : false;
    var hv = "ko";
    var sv = "ok";
    if (result) {
      var id = $(xml).find('id').text();
      $(fieldset).children('input[name=folder]').val('');
      var li = treeCreateElement(id, vFolder, 0);
      var ul = $("#tree-" + vParentId).next('ul');
      if ($(ul).length == 0) {
        ul = document.createElement('ul');
        $("#tree-" + vParentId).after(ul);
        $(ul).append(li);
      } else {
        $(ul).children('li:last').after(li);
      }
    } else {
      hv = "ok";
      sv = "ko";
    }
    $("#result-message span."+hv).hide();
    $("#result-message span."+sv).show();
    $("#result-message span."+sv).fadeOut(5000);
  });
}

$(document).ready(function() {
  $("#tree img").click(function() { treeExpand($(this).parent('div')); });
  $("#tree span").click(function() { treeSetup($(this).parent('div')); });
  $("#submit-new-folder").click( function() { submitNewFolder($(this).parent('fieldset')); } );
  $("form#new-folder").submit( function() { submitNewFolder($(this).children('fieldset')); return false; } );
});